package com.hongkzh.www.adv.service;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.hongkzh.www.conmon.BigNumberFormat;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author martins
 */
public class EnterpriseAdvService extends BaseService {


    public List<Record> getEnterpriseCategoryList() {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryList");
        List<Record> records = Db.find(sqlPara);
        return records;
    }

    public Page<Record> getEnterpriseCategoryAdvList(String cid, int pageNumber, String sortId, String sortValue) {
        if ("create_date".equals(sortId)) {
            sortId = " a.create_date ";
        } else if ("rank".equals(sortId)) {
            sortId = " a.img_rank ";
        }
        Kv cond = Kv.by("cid", cid).set("order", " order by " + sortId + " " + sortValue);
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryAdvList", cond);
        Page<Record> records = Db.paginate(pageNumber, 3, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;
    }

    public List<Record> getEnterpriseHotAdvTypeList() {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseHotAdvTypeList");
        List<Record> list = Db.find(sqlPara);
        int currentHour, beginDate, endDate;
        for (Record r : list) {
            r.set("typeId", r.get("id"));
            r.remove("id");
            currentHour = r.getInt("currentHour");

            beginDate = r.getInt("beginDate");
            endDate = r.getInt("endDate");

            Date currentTime = r.getTime("currentTime");
            Date beginTime = r.getTime("beginTime");
            Date endTime = r.getTime("endTime");
            r.set("sortDate", 1);
            if (currentTime.after(beginTime) && currentTime.before(endTime)) {
                r.set("sortDate", 0);
            } else {
                if (currentTime.before(beginTime)){
                    r.set("sortDate", -1);
                }
            }

//            if (currentHour >= beginDate && currentHour < endDate) {
//                r.set("sortDate", 0);
//            } else {
//                if (currentHour - beginDate > 0) {
//                    r.set("sortDate", currentHour - beginDate - 24);
//                }
//            }
        }
        Collections.sort(list, new Comparator<Record>() {
            //升序排序
            @Override
            public int compare(Record o1,
                               Record o2) {
                return o2.getInt("sortDate").compareTo(o1.getInt("sortDate"));
            }
        });

        return list;
    }

    public Page<Record> getEnterpriseHotAdvList(String id, int p) {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseHotAdvList", id);
        List<Record> records = Db.find(sqlPara);
        for (Record r : records) {
            String sourceId = r.getStr("id");
            Date beginDate = r.getTime("begin_date");
            Date endDate = r.getTime("end_date");
            SqlPara sqlGetPlaySet = Db.getSqlPara("enterprise.GetAdvPlaySetByIdAndDate", sourceId, beginDate, endDate);
            Record GetPlaySetRecord = Db.findFirst(sqlGetPlaySet);
            if (GetPlaySetRecord == null) {
//                Date play_date;
                String title = r.getStr("title");
                String subtitle = r.getStr("subtitle");
                Date beginTime = r.getTime("begin_date");
                Date endTime = r.getTime("end_date");
                String state = r.getStr("state");
                String remarks = r.getStr("remarks");
//                String delFlag = r.getStr("del_flag");
                String rank = r.getStr("rank");
                String advType = r.getStr("adv_type");
                String coverImgSrc = r.getStr("coverImgSrc");
                String imgRank = r.getStr("imgRank");
                int costCount = r.getInt("costCount");
                int integralCount = r.getInt("integralCount");
                int lastCount = r.getInt("lastCount");
                int percentage = r.getInt("percentage");
                String showType = r.getStr("showType");
                String content = r.getStr("content");
                String vedioLength = r.getStr("vedioLength");
                Date playTime = r.getTime("playTime");
                String uid = r.getStr("uid");
                int playCount = r.getInt("playCount");
                String currencyType = r.getStr("currencyType");
                String advId = r.getStr("enterpriseId");

                int integral = r.getInt("integral");

                Kv cond = Kv.create()
                        .set("title", title)
                        .set("subtitle", subtitle)
                        .set("begin_time", beginTime)
                        .set("end_time", endTime)
                        .set("remarks", remarks)
                        .set("rank", rank)
                        .set("sourceId", sourceId)
                        .set("advType", advType)
                        .set("coverImgSrc", coverImgSrc)
                        .set("imgRank", imgRank)
                        .set("costCount", costCount)
                        .set("integralCount", integralCount)
                        .set("lastCount", lastCount)
                        .set("percentage", percentage)
                        .set("showType", showType)
                        .set("content", content)
                        .set("vedioLength", vedioLength)
                        .set("playTime", playTime)
                        .set("playCount", playCount)
                        .set("currencyType", currencyType)
                        .set("integral", integral)
                        .set("advId", advId)
                        .set("user_id", uid);

                Db.update(Db.getSqlPara("enterprise.insertAdvPlaySet", cond
                ));
            }
        }
        SqlPara sqlHotTypePara = Db.getSqlPara("enterprise.getEenterpriseHotTypeList", id);
        Record sqlHotTypeParaRecord = Db.findFirst(sqlHotTypePara);
        Date beginTime = sqlHotTypeParaRecord.getTime("beginTime");
        Date endTime = sqlHotTypeParaRecord.getTime("endTime");
        Date now = new Date();
        Kv cond = new Kv().set("del", "0").set("burst", "and a.play_time   BETWEEN " + " '" + beginTime + "' " + "and " + " '" + endTime + "'");
        SqlPara sqlAdvPlaySetListPara = Db.getSqlPara("enterprise.getAdvPlaySetList", beginTime, endTime);
//        SqlPara sqlAdvPlaySetListPara = Db.getSqlPara("enterprise.getAdvPlaySetList", cond);
        Page<Record> returnRecords = Db.paginate(p, 10, sqlAdvPlaySetListPara);
        for (Record r : returnRecords.getList()) {
            doImgPath(r);
        }
        return returnRecords;
    }

//    public Page<Record> getEnterpriseHotAdvList(String id, int p) {
//        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseHotAdvList", id);
//        List<Record> records = Db.find(sqlPara);
//        for (Record r : records) {
//            String sourceId = r.getStr("enterprise_id");
//            SqlPara sqlGetPlaySet = Db.getSqlPara("enterprise.GetAdvPlaySetByIdAndDate", sourceId);
//            if (sqlGetPlaySet == null) {
//
////                Date play_date;
//                String title = r.getStr("title");
//                String subtitle = r.getStr("subtitle");
//                Date beginTime = r.getTime("begin_date");
//                Date endTime = r.getTime("end_date");
//                String state = r.getStr("state");
//                String remarks = r.getStr("remarks");
//                String delFlag = r.getStr("del_flag");
//                String rank = r.getStr("rank");
//                String advType = r.getStr("adv_type");
//                String coverImgSrc = r.getStr("cover_img_src");
//                String imgRank = r.getStr("img_rank");
//                int costCount = r.getInt("costCount");
//                int integralCount = r.getInt("integralCount");
//                int lastCount = r.getInt("lastCount");
//                int percentage = r.getInt("percentage");
//                String showType = r.getStr("showType");
//                String content = r.getStr("content");
//                int vedioLength = r.getInt("vedioLength");
//                Db.update(Db.getSqlPara("enterprise.insertAdvPlaySet", title,
//                        subtitle,
//                        beginTime,
//                        endTime,
//                        state,
//                        remarks,
//                        delFlag,
//                        rank,
//                        sourceId,
//                        advType,
//                        coverImgSrc,
//                        imgRank,
//                        costCount,
//                        integralCount,
//                        lastCount,
//                        percentage,
//                        showType,
//                        content,
//                        vedioLength));
//
//            }
//
//
//        }
//
//
////        Page<Record> records = Db.paginate(p, 6, sqlPara);
//
////        for (Record r : records.getList()) {
////            doImgPath(r);
////        }
//        return records;
//    }

    @Before(Tx.class)
    public String collection(String loginUid, String sid, String state) {
        if (StaticPublic.NO.equals(state)) {
            Db.update(Db.getSql("user.deleteCollection"), loginUid, sid);
        } else if (StaticPublic.YES.equals(state)) {
            String id = IdGen.uuid();
            Db.update(Db.getSql("user.collectionAdd"), id, sid, loginUid);
        }
        int counts = Db.findFirst(Db.getSqlPara("user.collectionList", sid)).getInt("counts");
        return BigNumberFormat.dataFormat(counts);
    }

    @Before(Tx.class)
    public String praise(String loginUid, String sid, String state) {
        if (StaticPublic.NO.equals(state)) {
            Db.update(Db.getSql("user.deletePraise"), loginUid, sid);
            Db.update(Db.getSqlPara("user.updateDataPlusPraise", sid));

        } else if (StaticPublic.YES.equals(state)) {
            String id = IdGen.uuid();
            int i = Db.update(Db.getSql("user.deletePraise"), loginUid, sid);
            Db.update(Db.getSql("user.praiseAdd"), id, sid, loginUid);
            if (i == 0) {
                Db.update(Db.getSqlPara("user.updateDataAddPraise", sid, id));
            }
        }
        int counts = Db.findFirst(Db.getSqlPara("user.praiseList", sid)).getInt("counts");
        return BigNumberFormat.dataFormat(counts);
    }

    public Page<Record> getEnterpriseReleaseAdvList(String enterpriseId, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseReleaseAdvList", enterpriseId);
        Page<Record> records = Db.paginate(pageNumber, 6, sqlPara);

        for (Record r : records.getList()) {

            doFilePath(r);
        }
        return records;
    }

    public Page<Record> getEnterpriseListByCategory(String categoryId, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseListByCategory", categoryId);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doImgPath(r);
        }
        return records;
    }
}
